<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

class SectionPeer extends BaseSectionPeer
{
	/**
	 * retrieves all Book Sections via Book id
	 * Does a SELECT * FROM book_sections WHERE book_id = :book_id
	 * @param $book_id, the book
	 * @return set of Section or null if not found.  ascending ordered by section_order
	 */
	public static function retrieveByBookId($book_id) {
		$c = new Criteria();
		$book_criterion = $c->getNewCriterion(SectionPeer::BOOK_ID, $book_id, Criteria::EQUAL);		
		// add to the Criteria
		$c->add($book_criterion);
		$c->addAscendingOrderByColumn(SectionPeer::SECTION_ORDER);
		
		// perform the select query and return section objects
		return SectionPeer::doSelect($c);
	}

	/**
	 * retrieves all Book Sections via Book id in section/subsection order
	 * Does a SELECT * FROM book_sections WHERE book_id = :book_id then reorders and gets subsections
	 * @param $book_id, the book
	 * @return set of Section with subsection arrays or null if not found.  ascending ordered by section_order and section's section_order
	 */
	public static function retrieveByBookIdInOrder($book_id) {
		$sections = array();
		$c = new Criteria();
		$book_criterion = $c->getNewCriterion(SectionPeer::BOOK_ID, $book_id, Criteria::EQUAL);	
		$parentid_criterion = $c->getNewCriterion(SectionPeer::PARENT_ID, NULL, Criteria::EQUAL);	
		// add to the Criteria
		$c->add($book_criterion);
		$c->add($parentid_criterion);
		$c->addAscendingOrderByColumn(SectionPeer::SECTION_ORDER);
		
		// perform the select query
		$secs = SectionPeer::doSelect($c);
		
		foreach($secs as $sec) {
			$sections[] = $sec;
			$children = SectionPeer::getChildren($sec->getId());
			if(count($children) > 0) {
				$sections[] = $children;
			}
		}
		return $sections;
	}

	public static function getChildren($id) {
		$relations = SectionRelationPeer::retrieveByParentId($id);
		$relarray = array();
		if($relations) {			
			foreach($relations as $relation) {
				$csec = SectionPeer::retrieveByPK($relation->getChildId());
				if($csec) {
					$relarray[] = $csec;
				}
			}
		}
		return $relarray;
	}
}
